﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Omu.AwesomeMvc;
using TCM;
using TCM.Models;

namespace TCM.Controllers{
    public class studentsMultiLookupController : Controller
    {
        private TCMContext Db = new TCMContext();
        public ActionResult GetItems(Guid[] v)
        {
            var items = Db.Students.AsEnumerable().Where(o => v != null && v.Contains(o.StudentID)).ToList();
            return Json(items.Select(student => new KeyContent(student.StudentID, student.Name)));
        }
        public ActionResult SearchForm()
        {
            return View();
        }

        public ActionResult Search(string search, Guid[] selected, int page)
        {
            const int PageSize = 10;
            selected = selected ?? new Guid[] { };
            search = (search ?? "").ToLower().Trim();

            var items = Db.Students.ToList().Skip((page - 1) * PageSize).Take(PageSize);//.Where(o => o.Name.ToLower().Contains(search) && (!selected.Contains(o.StudentID)));

            return Json(new AjaxListResult
            {
                Items = items.Select(o => new KeyContent(o.StudentID, o.Name.ToString())),
                More = items.Count() > page * PageSize
            });
        }
        public ActionResult SearchRegistrants(string search, Guid[] selected,Guid? SubjectID , int page)
        {
            const int PageSize = 10;
            selected = selected ?? new Guid[] { };
            search = (search ?? "").ToLower().Trim();

            var items = Db.Registrations.Where(p=>p.SubjectID==SubjectID).Select(l=>l.Student).AsEnumerable().Where(o => o.Name.ToLower().Contains(search) && !selected.Contains(o.StudentID)).ToList().Skip((page - 1) * PageSize).Take(PageSize);//.Where(o => o.Name.ToLower().Contains(search) && (!selected.Contains(o.StudentID)));

            return Json(new AjaxListResult
            {
                Items = items.Select(o => new KeyContent(o.StudentID, o.Name.ToString())),
                More = items.Count() > page * PageSize
            });
        }

        public ActionResult Selected(Guid[] selected)
        {
            var items = Db.Students.AsEnumerable().Where(o => (selected != null && selected.Contains(o.StudentID))).ToList();
            return Json(new AjaxListResult
            {
                Items = items.Select(o => new KeyContent(o.StudentID, o.Name.ToString()))
            });
        }
    }

     

}